#ifndef VSqlMetaModel_H
#define VSqlMetaModel_H

#include "VSqlMetaMap.h"
#include "VSqlDataMap.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class VSqlMetaModel : public QAbstractItemModel
{
	Q_OBJECT

public:
	VSqlMetaModel(const QSqlDatabase &db,QObject *parent = 0);
	~VSqlMetaModel();

	QSqlDatabase db() const { return m_db;}
	VSqlMetaMap *metaMap() const { return m_metaMap;}
	VSqlDataMap *dataMap() const { return m_dataMap;}
	
	void clear();
	void setRootRecord(const VSqlMeta &v);

	VSqlMetaRecord *recordFromIndex(const QModelIndex &index) const;
	QModelIndex indexFromRecord(VSqlMetaRecord *record) const;

public: //QAbstractItemModel
	QModelIndex index(int row,int column,const QModelIndex &parent = QModelIndex()) const;
	QModelIndex parent(const QModelIndex &index) const;
	
	bool hasChildren (const QModelIndex &parent = QModelIndex()) const;
	bool canFetchMore(const QModelIndex &parent) const;
	void fetchMore   (const QModelIndex &parent);
	
	QVariant headerData(int section,Qt::Orientation orientation,int role = Qt::DisplayRole) const;

	int rowCount   (const QModelIndex &parent = QModelIndex()) const;
	int columnCount(const QModelIndex &parent = QModelIndex()) const;

	Qt::ItemFlags flags(const QModelIndex &index) const;

	QVariant data(const QModelIndex &index,int role) const;
	bool  setData(const QModelIndex &index,const QVariant &value,int role = Qt::EditRole);
	
private:
	QSqlDatabase m_db;
	VSqlMetaMap *m_metaMap;
	VSqlDataMap *m_dataMap;

	VSqlMetaRecord *m_rootRecord;
};

///////////////////////////////////////////////////////////////////////////////

#endif
